Skip to main content
ICT
Lesson A20 - Inheritance, Polymorphism, and Abstract Classes
 
Main Previous Next
Lesson A1 >  
Lesson A2 >  
Lesson A3 >  
Lesson A4 >  
Lesson A5 >  
Lesson A6 >  
Lesson A7 >  
Lesson A8 >  
Lesson A9 >  
Lesson A10 >  
Lesson A11 >  
Lesson A12 >  
Lesson A13 >  
Lesson A14 >  
Lesson A15 >  
Lesson A16 >  
Lesson A17 >  
Lesson A18 >  
Lesson A19 >  
Lesson A20 >  
Lesson A21 >  
Lesson A22 >  
Lesson AB23 >  
Lesson AB24 >  
Lesson AB25 >  
Lesson AB26 >  
Lesson AB27 >  
Lesson AB28 >  
Lesson AB29 >  
Lesson AB30 >  
Lesson AB31 >  
Lesson AB32 >  
Lesson AB33 >  
 

C. Polymorphism page 5 of 8

  1. In addition to facilitating the re-use of code, inheritance provides a common base data type that lets us refer to objects of specific types through more generic types of references; in particular, we can mix objects of different subtypes in the same collection. For example:

    SeaCreature s = new Fish(...);
    ...
    s.swim();

    The data type of an instance of the Fish class is a Fish, but it is also a kind of SeaCreature. Java provides the ability to refer to a specific type through more generic types of references.

  2. There may be situations that require a reference to an object using its more generic supertype rather than its most specific type. One such situation is when different subtypes of objects in the same collection (array, list, etc.) are mixed. For example:

    ArrayList <SeaCreature> creature = new ArrayList <SeaCreature>;
    Creature.add( new Fish(...));
    Creature.add( new Mermaid(...));
    ...
    creature.get(currentCreature).swim();

    This is possible because both Fish and Mermaid are SeaCreatures.

  3. Note that the Fish and Mermaid classes provide two different implementations of the swim method. The correct method that belongs to the class of the actual object is located by the Java virtual machine. That means that one method call

    String s = x.getname();

    can call different methods depending on the current reference of x. When a subclass redefines the implementation of a method from its superclass, it is called overriding the method. Note that for overridden methods in Java, the actual method to call is always determined at run time. This is called dynamic binding or late binding.

  4. The principle that the actual type of the object determines the method to be called is polymorphism (Greek for “many shapes”). The same computation works for objects of many forms and adapts itself to the nature of the objects.

  5. Overloading a method is often confused with overriding a method because the names are so similar. Overloading a method means to keep the name of the method, but change the parameter list. In this case, the compiler can choose the method that is actually called because the signatures are different. The Math class has many examples of overloaded methods. For example Math.max(double a, double b) and Math.max(int a, int b) are overloaded versions of the max method. The compiler determines which one to call depending on the type of the arguments that are being passed in.

 

Main Previous Next
Contact
 © ICT 2006, All Rights Reserved.